import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2023-03-08
 * Time: 20:12
 */
public class MyStack {
    public int[] elem;
    public int useSize;

    public MyStack() {
        this.elem = new int[5];
    }

    public void push(int val) {
        if(isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.useSize] = val;
        useSize++;
    }
    public boolean isFull() {
        return this.useSize == this.elem.length;
    }

    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        return this.elem[useSize - 1];
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        int oldVal = this.elem[useSize-1];
        this.useSize--;
        return oldVal;
    }

    public boolean isEmpty() {
        return this.useSize == 0;
    }
}
